package com.tianma315.core.small_code.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.tianma315.core.common.base.CoreServiceImpl;
import com.tianma315.core.common.exception.MessageException;
import com.tianma315.core.small_code.dao.SmallRecordDao;
import com.tianma315.core.small_code.domain.SmallRecordDO;
import com.tianma315.core.small_code.service.SmallRecordService;
import com.tianma315.core.small_code.vo.SmallRecordListVO;
import com.tianma315.core.small_code.vo.SmallRecordVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 
 * <pre>
 * 外包装管理记录
 * </pre>
 * <small> 2019-08-10 10:06:48 | Wen</small>
 */
@Service
public class SmallRecordServiceImpl extends CoreServiceImpl<SmallRecordDao, SmallRecordDO> implements SmallRecordService {

    @Override
    public Boolean addSmallRecord(SmallRecordVO smallRecordVO) {
//        smallRecordVO.setCreateDate(Calendar.getInstance().getTime());
        List<String> codes = smallRecordVO.getSmallCodes();
        Boolean check = checkSmallRecord(smallRecordVO.getSmallCode(),smallRecordVO.getMerchantId());
        Boolean codeCheck = checkCode(smallRecordVO.getSmallCodes(),smallRecordVO.getMerchantId());
        if (check && codeCheck){
            for (int i = 0; i < codes.size(); i++) {
                String code =codes.get(i);
                SmallRecordDO smallRecordDO = new SmallRecordDO();

                BeanUtils.copyProperties(smallRecordVO,smallRecordDO);
                smallRecordDO.setSmallCode(code);
                insert(smallRecordDO);
            }
        }else{
            throw new MessageException("外箱码或者产品码不能重复");
        }
        return true;
    }
//
//    @Override
//    public Boolean addSmallRecordAndStock(SmallRecordVO smallRecordVO) {
//        smallRecordVO.setCreateDate(Calendar.getInstance().getTime());
//        List<String> codes = smallRecordVO.getSmallCodes();
//        Boolean check = checkSmallRecord(smallRecordVO.getSmallCode(),smallRecordVO.getCompanyId());
//        Boolean codeCheck = checkCode(smallRecordVO.getSmallCodes(),smallRecordVO.getCompanyId());
//        if (check && codeCheck){
//            for (int i = 0; i < codes.size(); i++) {
//                String code =codes.get(i);
//                SmallRecordDO smallRecordDO = new SmallRecordDO();
//
//                BeanUtils.copyProperties(smallRecordVO,smallRecordDO);
//                smallRecordDO.setSmallCode(code);
//                insert(smallRecordDO);
//            }
//        }else{
//            throw new MessageException("外箱码或者产品码不能重复");
//        }
//        String smallCode = smallRecordVO.getSmallCode();
//        Integer productId = smallRecordVO.getProductId();
//        WarehouseRecordDO warehouseRecordDO = new WarehouseRecordDO();
//        warehouseRecordDO.setCode(smallCode);
//        warehouseRecordDO.setProductId(productId);
////        warehouseRecordDO.setType(1);
//        warehouseRecordDO.setCreateBy(smallRecordVO.getCreateBy());
//        warehouseRecordDO.setCreateDate(Calendar.getInstance().getTime());
//        warehouseRecordService.insert(warehouseRecordDO);
//
////        StockDO stockDO = new StockDO();
////        stockDO.setCompanyId(smallRecordVO.getMerchantId());
////        stockDO.setSmallNumber();
//
//        EntityWrapper<StockDO> entityWrapper = new EntityWrapper<>();
//        entityWrapper.eq("product_id",productId);
//        entityWrapper.eq("company_id",smallRecordVO.getCompanyId());
//        StockDO stockDO = stockService.selectOne(entityWrapper);
//        if (stockDO == null){
//            stockDO = new StockDO();
//            stockDO.setCompanyId(smallRecordVO.getCompanyId());
//            stockDO.setProductId(productId.longValue());
//            stockDO.setBigNumber(1);
//            stockService.insert(stockDO);
//        }else{
//            stockDO.setBigNumber(stockDO.getBigNumber()+1);
//            stockService.updateById(stockDO);
//        }
//
//        WarehouseRecordCodeDO warehouseRecordCodeDO = new WarehouseRecordCodeDO();
//        warehouseRecordCodeDO.setBigCode(smallRecordVO.getBigCode());
//        warehouseRecordCodeDO.setWarehouseRecordId(warehouseRecordDO.getId());
//        warehouseRecordCodeService.insert(warehouseRecordCodeDO);
//        return true;
//    }

    @Override
    public SmallRecordListVO getSmallRecordListVOByCode(String code) {
        EntityWrapper codeWrapper = new EntityWrapper();
        codeWrapper.eq("code",code);
//        codeWrapper.eq("merchant_id",companyId);
        SmallRecordListVO smallRecordListVO = new SmallRecordListVO();
        SmallRecordDO smallRecordDO = selectOne(codeWrapper);
        if (smallRecordDO != null){
            smallRecordListVO = getSmallRecordListVOBySmallCode(smallRecordDO.getSmallCode());
        }

        return smallRecordListVO;
    }

    @Override
    public SmallRecordListVO getSmallRecordListVOBySmallCode(String smallCode) {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("small_code",smallCode);
//        entityWrapper.eq("merchant_id",companyId);
        SmallRecordListVO smallRecordListVO = new SmallRecordListVO();
        List<SmallRecordDO> smallRecordDOS = selectList(entityWrapper);
        if (CollectionUtils.isNotEmpty(smallRecordDOS)){
            List<String> code = smallRecordDOS.stream().map(item -> item.getSmallCode()).collect(Collectors.toList());
            smallRecordListVO.setSmallCodes(code);
            smallRecordListVO.setBigCode(smallCode);
        }

        return smallRecordListVO;
    }

    public Boolean checkSmallRecord(String bigCode,Long companyId){
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq("big_code",bigCode);
//        wrapper.eq("merchant_id",companyId);
        List<SmallRecordDO> smallRecordDOS = selectList(wrapper);
        Boolean result = false;
        if (CollectionUtils.isEmpty(smallRecordDOS)){
            result = true;
        }
        return result;
    }

    public Boolean checkCode(List<String> smallCode,Long companyId){
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.in("small_code",smallCode);
//        wrapper.eq("merchant_id",companyId);
        List<SmallRecordDO> smallRecordDOList = selectList(wrapper);
        Boolean result = false;
        if (CollectionUtils.isEmpty(smallRecordDOList)){
            result = true;
        }
        return result;
    }
}
